package zhihui.mobile.ui.express;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.express.ExpressBeanListV2;
import zhihui.mobile.bean.shop.SkuInfo;
import zhihui.mobile.bean.shop.order.ShopOrderDetail;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.shop.ShopOrderManager;
import zhihui.mobile.ui.shop.order.ShopOrderDetailsActivity;

/**
 * Created by Administrator on 2017/5/6 0006.
 */

public class ReceiveExpressAdapter extends BaseQuickAdapter<ExpressBeanListV2.DataBean.InnerBean, BaseViewHolder> {

    private Activity context;

    public ReceiveExpressAdapter(List data, Activity context) {
        super(R.layout.item_receive_express, data);
        this.context = context;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final ExpressBeanListV2.DataBean.InnerBean item) {
        helper.setText(R.id.item_express_receive_orderid_tv, "快递单号：" + item.getOrdersn_f());
        helper.setText(R.id.item_express_receive_time_tv, "入库时间：" + item.getTime_f());
        helper.setText(R.id.item_express_receive_getcode_tv, "取件码：" + item.getNo_f());
        helper.addOnClickListener(R.id.item_express_receive_details_btn);
        if (item.getEname_f() == null || item.getEname_f().isEmpty()) {
            helper.setText(R.id.item_express_receive_name_tv, "快递公司：" + "智惠妈妈");
            helper.setVisible(R.id.item_express_receive_orderdetails_layout, true);
        } else {
            helper.setText(R.id.item_express_receive_name_tv, "快递公司：" + item.getEname_f());
            helper.setVisible(R.id.item_express_receive_orderdetails_layout, false);
            if (item.getEname_f().equals("智惠妈妈")) {
                helper.setVisible(R.id.item_express_receive_orderdetails_layout, true);
            }
        }
        //派送状态
        switch (item.getStatus_f()) {
            case 0:
            case 4:
                helper.setVisible(R.id.item_express_receive_status_tv, false);
                helper.setVisible(R.id.item_check_cb, true);
                break;
            case 2:
                helper.setVisible(R.id.item_express_receive_status_tv, true);
                helper.setVisible(R.id.item_check_cb, false);
                helper.setVisible(R.id.item_express_receive_orderdetails_layout, false);
                helper.setText(R.id.item_express_receive_status_tv, "待派送");
                break;
            case 3:
                helper.setVisible(R.id.item_express_receive_status_tv, true);
                helper.setVisible(R.id.item_check_cb, false);
                helper.setVisible(R.id.item_express_receive_orderdetails_layout, false);
                helper.setText(R.id.item_express_receive_status_tv, "派送中");
                break;
        }


        CheckBox checkBox = helper.getView(R.id.item_check_cb);
        if (item.isCheck()) {
            checkBox.setChecked(true);
        } else {
            checkBox.setChecked(false);
        }
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mData.get(helper.getLayoutPosition()).setCheck(isChecked);
            }
        });
        helper.addOnClickListener(R.id.item_check_cb);

        //派送历史
        switch (item.getStatus_f()) {
            case 0:
            case 2:
            case 3:
                helper.setVisible(R.id.item_express_receive_details_btn, false);
                break;
            case 4:
                helper.setVisible(R.id.item_express_receive_details_btn, true);
                break;
        }
        helper.getView(R.id.item_express_receive_details_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ReceiveExpressDetailsDialog dialog = new ReceiveExpressDetailsDialog(context, "" + mData.get(helper.getLayoutPosition()).getId_f());
                dialog.createDialog();
            }
        });
        helper.getView(R.id.item_express_receive_orderdetails_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShopOrderManager.getInstance().getOrderDetail(new IHttpCallBackListener() {
                    @Override
                    public void success(JSONObject result) {
                        ShopOrderDetail detail;
                        try {
                            JSONObject data = result.getJSONObject("data");
                            Gson gson = new Gson();
                            detail = gson.fromJson(data.getJSONObject("orderInfo").toString(), ShopOrderDetail.class);
                            List<SkuInfo> list = gson.fromJson(data.getJSONArray("goodsList").toString(), new TypeToken<List<SkuInfo>>() {
                            }.getType());
                            detail.setGoodsList(list);
                            ShopOrderManager.getInstance().setCurOrderDetail(detail);
                            Intent intent = new Intent(context, ShopOrderDetailsActivity.class);
                            context.startActivity(intent);
                        } catch (JSONException e) {
                            e.printStackTrace();
                            com.zgm.utils.UiUtils.ToastInCenter(context, "解析数据失败", Toast.LENGTH_SHORT);
                        }
                    }

                    @Override
                    public void failed(String message) {
                        com.zgm.utils.UiUtils.ToastInCenter(context, message, Toast.LENGTH_SHORT);
                    }
                }, mData.get(helper.getLayoutPosition()).getOrdersn_f());
            }
        });

    }
}
